﻿/*
 * 登录窗口
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WH.NurseSys.Model;
using WH.NurseSys.BLL;

namespace WH.NurseSys.Report
{
    public partial class LoginForm : Form
    {
        DoctorBLL doctorBLL = new DoctorBLL();

        public LoginForm()
        {
            InitializeComponent();
        }

        private void buttonConfig_Click(object sender, EventArgs e)
        {
            ConfigForm configForm = new ConfigForm();
            configForm.ShowDialog();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            string username = this.textBoxUsername.Text;
            string password = this.textBoxPassword.Text;
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                this.labelMsg.Text = "输入的帐号信息无效";
            }
            else
            {
                this.labelMsg.Text = "正在登录中...";
            }
            progressBar.Style = ProgressBarStyle.Marquee;
            buttonLogin.Enabled = false;
            backgroundWorkerLogin.RunWorkerAsync(new string[]{username,password});
        }

        private void backgroundWorkerLogin_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] values = e.Argument as string[];
            int code;
            Doctor doctor = doctorBLL.Login(values[0], values[1], out code);
            if (code == 1)
            {
                Dept dept=new DeptBLL().GetById(doctor.DeptId);
                ConfigHelper.SetAppSetting("userId",doctor.Id.ToString());
                ConfigHelper.SetAppSetting("nickname", doctor.Nickname);
                ConfigHelper.SetAppSetting("deptId", doctor.DeptId.ToString());
                ConfigHelper.SetAppSetting("deptName", dept.Name);
                ConfigHelper.SetAppSetting("rank", doctor.Rank.ToString());
            }
            e.Result = code;
        }

        private void backgroundWorkerLogin_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar.Style = ProgressBarStyle.Continuous;
            buttonLogin.Enabled = true;
            int code = (int)e.Result;
            if (code == 1)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else if (code == 2)
                this.labelMsg.Text = "该用户不存在";
            else if (code == 3)
                this.labelMsg.Text = "密码错误";
        }
    }
}
